<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyAPI extends CI_Controller {

	//Default function for the API
	public function index()
	{
		$this->load->view('v_myapi');
	}
	
	//A function
	public function function1()
	{
		$jsonObject->var1 = "FOO";
		$jsonObject->var2 = 3;
		$jsonObject->var3 = 8.2;
		
		header('Content-type: application/json');
		echo json_encode($jsonObject);
	}
	
	//Another function
	public function function2()
	{
		$temp1->a = 2.4;
		$temp2->b = 287.2;
		$temp2->a = 29;
		$temp2->b = 198;
		$temp3->a = "letter a";
		$temp3->b = "letter b";
		$temp3->c = "letter c";
		
		$jsonObject->status = "SUCCESS";
		$jsonObject->myarray = Array();
		$jsonObject->myarray[0] = $temp1;
		$jsonObject->myarray[1] = $temp2;
		array_push($jsonObject->myarray, $temp3);
		
		header('Content-type: application/json');
		echo json_encode($jsonObject);
	}
	
	//Another function
	//Read http://codeigniter.com/user_guide/libraries/input.html
	public function function3()
	{
		if($this->input->get('mygetvar1', TRUE))
		{
			$temp = $this->input->get('mygetvar1', TRUE);
			
			$jsonObject->status = "Success";
			$jsonObject->response_payload = $temp + 1;
			
			header('Content-type: application/json');
			echo json_encode($jsonObject);
		}
		else
		{
			$jsonObject->status = "ERROR";
			$jsonObject->error_description = "mygetvar1 not set";
			
			header('Content-type: application/json');
			echo json_encode($jsonObject);
		}
	}
	
	//Another function
	//Read http://codeigniter.com/user_guide/libraries/input.html
	public function function4()
	{
		if($this->input->post('mypostvar1', TRUE))
		{
			$temp = $this->input->post('mypostvar1', TRUE);
			
			$jsonObject->status = "Success";
			$jsonObject->response_payload = $temp + 1;
			
			header('Content-type: application/json');
			echo json_encode($jsonObject);
		}
		else
		{
			$jsonObject->status = "ERROR";
			$jsonObject->error_description = "mypostvar1 not set";
			
			header('Content-type: application/json');
			echo json_encode($jsonObject);
		}
	}
	
	//Another function
	//Sends a query to another webservice
	public function function5()
	{
		$url = site_url("myapi/function1");
		
		$json = NULL;
		
		if(function_exists("curl_init"))
		{
			$curl_handle = curl_init();
			curl_setopt ($curl_handle, CURLOPT_URL, $url);
			curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, TRUE);
			$json = curl_exec($curl_handle);
			curl_close($curl_handle);
		}
		else
		{
			$json = file_get_contents($url);
		}
		
		var_dump(json_decode($json));
	}
	
	//Another function
	//Sends a get query to another webservice
	public function function6()
	{
		$url = site_url("myapi/function3?mygetvar1=4");
		
		$json = NULL;
		
		if(function_exists("curl_init"))
		{
			$curl_handle = curl_init();
			curl_setopt ($curl_handle, CURLOPT_URL, $url);
			curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, TRUE);
			$json = curl_exec($curl_handle);
			curl_close($curl_handle);
		}
		else
		{
			$json = file_get_contents($url);
		}
		
		var_dump(json_decode($json));
	}
	
	//Another function
	//Sends a post query to another webservice
	public function function7()
	{
		$url = site_url("myapi/function4");
		
		$postdata = http_build_query(
			array(
				'mypostvar1' => '4'
			)
		);
		
		$json = NULL;

		if(function_exists("curl_init"))
		{
			$curl_handle = curl_init();
			curl_setopt ($curl_handle, CURLOPT_POST, TRUE);
			curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $postdata);
			curl_setopt ($curl_handle, CURLOPT_URL, $url);
			curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, TRUE);
			$json = curl_exec($curl_handle);
			curl_close($curl_handle);
		}
		else
		{
			$opts = array('http' =>
				array(
					'method'  => 'POST',
					'header'  => 'Content-type: application/x-www-form-urlencoded',
					'content' => $postdata
				)
			);
			
			$context  = stream_context_create($opts);

			$json = file_get_contents($url, false, $context);
		}
		
		var_dump(json_decode($json));
	}
}